<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="别名" prop="alias">
        <el-input
          v-model="queryParams.alias"
          placeholder="请输入别名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老人编号" prop="oldManNumber">
        <el-input
          v-model="queryParams.oldManNumber"
          placeholder="请输入老人编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="固定电话" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入固定电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号码" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入证件号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生日期" prop="dateBirth">
        <el-date-picker clearable
          v-model="queryParams.dateBirth"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="农历生日" prop="lunarBirthday">
        <el-date-picker clearable
          v-model="queryParams.lunarBirthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择农历生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="户籍地址-详情" prop="permanentAddress">
        <el-input
          v-model="queryParams.permanentAddress"
          placeholder="请输入户籍地址-详情"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现居地址-详情" prop="residenceAddress">
        <el-input
          v-model="queryParams.residenceAddress"
          placeholder="请输入现居地址-详情"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="民族" prop="nation">
        <el-input
          v-model="queryParams.nation"
          placeholder="请输入民族"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="个人特长" prop="personalSpecialty">
        <el-input
          v-model="queryParams.personalSpecialty"
          placeholder="请输入个人特长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="爱好" prop="hobby">
        <el-input
          v-model="queryParams.hobby"
          placeholder="请输入爱好"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子女数" prop="quantityChildren">
        <el-input
          v-model="queryParams.quantityChildren"
          placeholder="请输入子女数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:message:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:message:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:message:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:message:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="老人id" align="center" prop="oldManId" />
      <el-table-column label="别名" align="center" prop="alias" />
      <el-table-column label="老人编号" align="center" prop="oldManNumber" />
      <el-table-column label="固定电话" align="center" prop="telephone" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="性别(0,1)" align="center" prop="isGender" />
      <el-table-column label="证件类型" align="center" prop="certificateType" />
      <el-table-column label="证件号码" align="center" prop="idNumber" />
      <el-table-column label="出生日期" align="center" prop="dateBirth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateBirth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="农历生日" align="center" prop="lunarBirthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lunarBirthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="户籍地址-详情" align="center" prop="permanentAddress" />
      <el-table-column label="户籍-省" align="center" prop="permanentProvince" />
      <el-table-column label="户籍-城市" align="center" prop="permanentCity" />
      <el-table-column label="户籍-区" align="center" prop="permanentArea" />
      <el-table-column label="现居地址-详情" align="center" prop="residenceAddress" />
      <el-table-column label="现居地址-省" align="center" prop="residenceProvince" />
      <el-table-column label="现居地址-城市" align="center" prop="residenceCity" />
      <el-table-column label="现居地址-城市" align="center" prop="residenceArea" />
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="婚姻状况" align="center" prop="maritalStatus" />
      <el-table-column label="文化程度" align="center" prop="standardCulture" />
      <el-table-column label="职业" align="center" prop="occupation" />
      <el-table-column label="退休(0,1)" align="center" prop="isRetire" />
      <el-table-column label="个人特长" align="center" prop="personalSpecialty" />
      <el-table-column label="性格(0,1)" align="center" prop="isCharacter" />
      <el-table-column label="爱好" align="center" prop="hobby" />
      <el-table-column label="子女数" align="center" prop="quantityChildren" />
      <el-table-column label="健在(0,1)" align="center" prop="isSurvival" />
      <el-table-column label="政府救助类型" align="center" prop="governmentRescue" />
      <el-table-column label="主要经济来源" align="center" prop="sourceFinance" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:message:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:message:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老人信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="别名" prop="alias">
          <el-input v-model="form.alias" placeholder="请输入别名" />
        </el-form-item>
        <el-form-item label="老人编号" prop="oldManNumber">
          <el-input v-model="form.oldManNumber" placeholder="请输入老人编号" />
        </el-form-item>
        <el-form-item label="固定电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入固定电话" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="证件号码" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="出生日期" prop="dateBirth">
          <el-date-picker clearable
            v-model="form.dateBirth"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="农历生日" prop="lunarBirthday">
          <el-date-picker clearable
            v-model="form.lunarBirthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择农历生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="户籍地址-详情" prop="permanentAddress">
          <el-input v-model="form.permanentAddress" placeholder="请输入户籍地址-详情" />
        </el-form-item>
        <el-form-item label="现居地址-详情" prop="residenceAddress">
          <el-input v-model="form.residenceAddress" placeholder="请输入现居地址-详情" />
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="个人特长" prop="personalSpecialty">
          <el-input v-model="form.personalSpecialty" placeholder="请输入个人特长" />
        </el-form-item>
        <el-form-item label="爱好" prop="hobby">
          <el-input v-model="form.hobby" placeholder="请输入爱好" />
        </el-form-item>
        <el-form-item label="子女数" prop="quantityChildren">
          <el-input v-model="form.quantityChildren" placeholder="请输入子女数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMessage, getMessage, delMessage, addMessage, updateMessage } from "@/api/cluster/message";

export default {
  name: "Message",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老人信息表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        alias: null,
        oldManNumber: null,
        telephone: null,
        phone: null,
        isGender: null,
        certificateType: null,
        idNumber: null,
        dateBirth: null,
        lunarBirthday: null,
        permanentAddress: null,
        permanentProvince: null,
        permanentCity: null,
        permanentArea: null,
        residenceAddress: null,
        residenceProvince: null,
        residenceCity: null,
        residenceArea: null,
        nation: null,
        maritalStatus: null,
        standardCulture: null,
        occupation: null,
        isRetire: null,
        personalSpecialty: null,
        isCharacter: null,
        hobby: null,
        quantityChildren: null,
        isSurvival: null,
        governmentRescue: null,
        sourceFinance: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        isGender: [
          { required: true, message: "性别(0,1)不能为空", trigger: "change" }
        ],
        certificateType: [
          { required: true, message: "证件类型不能为空", trigger: "change" }
        ],
        idNumber: [
          { required: true, message: "证件号码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询老人信息列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        oldManId: null,
        alias: null,
        oldManNumber: null,
        telephone: null,
        phone: null,
        isGender: null,
        certificateType: null,
        idNumber: null,
        dateBirth: null,
        lunarBirthday: null,
        permanentAddress: null,
        permanentProvince: null,
        permanentCity: null,
        permanentArea: null,
        residenceAddress: null,
        residenceProvince: null,
        residenceCity: null,
        residenceArea: null,
        nation: null,
        maritalStatus: null,
        standardCulture: null,
        occupation: null,
        isRetire: null,
        personalSpecialty: null,
        isCharacter: null,
        hobby: null,
        quantityChildren: null,
        isSurvival: null,
        governmentRescue: null,
        sourceFinance: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.oldManId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加老人信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const oldManId = row.oldManId || this.ids
      getMessage(oldManId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老人信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.oldManId != null) {
            updateMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const oldManIds = row.oldManId || this.ids;
      this.$modal.confirm('是否确认删除老人信息编号为"' + oldManIds + '"的数据项？').then(function() {
        return delMessage(oldManIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
